package com.tokenplus.thai.coupons.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.tokenplus.thai.R
import com.tokenplus.thai.adata.coupons.RevCouponItem
import com.tokenplus.thai.extend.toFinanceString

class CouponsAdapter(private val dataList: List<RevCouponItem>) :
    RecyclerView.Adapter<CouponsAdapter.ViewHolder>() {

    //点击事件
    private var mItemClickListener: (RevCouponItem) -> Unit = {}

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var rlAdapterCouponsContent: RelativeLayout =
            view.findViewById(R.id.rlAdapterCouponsContent)
        var textAdapterCouponsFlag: TextView =
            view.findViewById(R.id.textAdapterCouponsFlag)
        var textAdapterCouponsAmount: TextView =
            view.findViewById(R.id.textAdapterCouponsAmount)
        var textAdapterCouponsCondition: TextView =
            view.findViewById(R.id.textAdapterCouponsCondition)
        var textAdapterCouponsExpired: TextView =
            view.findViewById(R.id.textAdapterCouponsExpired)
        var textAdapterCouponsUse: TextView =
            view.findViewById(R.id.textAdapterCouponsUse)
        var imageAdapterCoupons: ImageView =
            view.findViewById(R.id.imageAdapterCoupons)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_coupons, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n", "StringFormatInvalid")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevCouponItem = dataList[position]
        holder.textAdapterCouponsFlag.text = data.tag
        holder.textAdapterCouponsAmount.text = data.amount?.toFinanceString()
        holder.textAdapterCouponsCondition.text = data.condition
        holder.textAdapterCouponsExpired.text =
            holder.textAdapterCouponsExpired.context.getString(
                R.string.coupon7,
                data.expiredDate ?: ""
            )

        // 状态CREATED:未使用,EXPIRED:已过期,USED:已使用
        val status = data.status ?: ""
        if (status.contains("EXPIRED", false)) {
            holder.rlAdapterCouponsContent.setBackgroundResource(R.drawable.coupon_mode_expired)
            holder.textAdapterCouponsFlag.setBackgroundResource(R.drawable.coupon_flag_enable)
            holder.textAdapterCouponsFlag.setTextColor(Color.parseColor("#800184E6"))
            holder.imageAdapterCoupons.visibility = View.VISIBLE
            holder.textAdapterCouponsUse.visibility = View.GONE
            holder.imageAdapterCoupons.setImageResource(R.drawable.icon_coupon_expired)
            holder.textAdapterCouponsUse.setOnClickListener {
            }

        } else if (status.contains("USED", false)) {
            holder.rlAdapterCouponsContent.setBackgroundResource(R.drawable.coupon_mode_expired)
            holder.textAdapterCouponsFlag.setBackgroundResource(R.drawable.coupon_flag_enable)
            holder.textAdapterCouponsFlag.setTextColor(Color.parseColor("#800184E6"))
            holder.imageAdapterCoupons.visibility = View.VISIBLE
            holder.textAdapterCouponsUse.visibility = View.GONE
            holder.imageAdapterCoupons.setImageResource(R.drawable.icon_coupon_used)
            holder.textAdapterCouponsUse.setOnClickListener {
            }

        } else if (status.contains("unable", false)) {
            holder.rlAdapterCouponsContent.setBackgroundResource(R.drawable.coupon_mode_unable)
            holder.textAdapterCouponsFlag.setBackgroundResource(R.drawable.coupon_flag_unable)
            holder.textAdapterCouponsFlag.setTextColor(Color.parseColor("#777D90"))
            holder.imageAdapterCoupons.visibility = View.GONE
            holder.textAdapterCouponsUse.setOnClickListener {
            }

        } else if (status.contains("CREATED", false)) {
            holder.rlAdapterCouponsContent.setBackgroundResource(R.drawable.coupon_mode_use)
            holder.textAdapterCouponsFlag.setBackgroundResource(R.drawable.coupon_flag_enable)
            holder.textAdapterCouponsFlag.setTextColor(Color.parseColor("#0184E6"))
            holder.textAdapterCouponsUse.setTextColor(Color.parseColor("#2854F6"))
            holder.textAdapterCouponsCondition.setTextColor(Color.parseColor("#FFFFFF"))
            holder.imageAdapterCoupons.visibility = View.GONE
            holder.textAdapterCouponsUse.visibility = View.VISIBLE

            if (data.useFlag) {
                holder.textAdapterCouponsUse.setOnClickListener {
                    mItemClickListener.invoke(data)
                }
            } else {
                holder.rlAdapterCouponsContent.setBackgroundResource(R.drawable.coupon_mode_unable)
                holder.textAdapterCouponsFlag.setBackgroundResource(R.drawable.coupon_flag_unable)
                holder.textAdapterCouponsFlag.setTextColor(Color.parseColor("#777D90"))
                holder.textAdapterCouponsUse.setTextColor(Color.parseColor("#777D90"))
                holder.textAdapterCouponsCondition.setTextColor(Color.parseColor("#FF0000"))
                holder.imageAdapterCoupons.visibility = View.GONE
                holder.textAdapterCouponsUse.setOnClickListener {
                    val context = holder.textAdapterCouponsUse.context
                    Toast.makeText(
                        context,
                        context.getString(R.string.coupon9),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (RevCouponItem) -> Unit) {
        mItemClickListener = listener
    }
}